﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class activity : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int bid = 0;
            int.TryParse(Request.QueryString["id"], out bid);
            DTO.BookClub club = BookClubProcessorInstance.GetBookClubById(bid);
            lblClubName.Text = club.Name;

            DTO.Activities activities = BookClubProcessorInstance.GetActivityById(bid);

            grdActivityGrid.DataSource = activities;
            grdActivityGrid.DataBind();

        }

        public void BindData(DTO.Activities activities)
        {
            grdActivityGrid.DataSource = activities;
            grdActivityGrid.DataBind();
        }

        protected void grdActivityGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            DTO.Activity activity = e.Row.DataItem as DTO.Activity;

            int tid = 0;
            int.TryParse(Request.QueryString["tid"], out tid);

            string username = HttpContext.Current.User.Identity.Name.ToString();
            DTO.Member memberbyusername = MemberProcessorInstance.GetMemberByUsername(username);
            int CurrentUserId = memberbyusername.Id;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink lnkMember = ((HyperLink)e.Row.FindControl("lnkMember"));
                Image imgMemberAvatar = ((Image)e.Row.FindControl("imgMemberAvatar"));
                Label lblTypeAction = ((Label)e.Row.FindControl("lblTypeAction"));
                Label lblActionDate = ((Label)e.Row.FindControl("lblActionDate"));

                DTO.Member member = MemberProcessorInstance.GetMemberById(activity.User_Id);

              //  ADDMEETING,
              //  ADDPOLL,

                if (activity.Type == "JOINED")
                {
                    lblTypeAction.Text = "joined this book club.";
                }
                 if (activity.Type == "ADDLEADER")
                {
                    lblTypeAction.Text = "became a leader.";
                }
                if (activity.Type == "CHANGEBOOK")
                {
                    DTO.Book book = BookProcessorInstance.GetBookById(activity.Data);
                    lblTypeAction.Text = "changed the current book to <a href='../book.aspx?id=" + book.Id +"'>"+ book.Title +"</a>";
                }
                if (activity.Type == "REMOVEBOOK")
                {
                    DTO.Book book = BookProcessorInstance.GetBookById(activity.Data);
                    lblTypeAction.Text = "removed the book <a href='../book.aspx?id=" + book.Id + "'>" + book.Title + "</a> from the shelf.";
                }
                if (activity.Type == "CREATED")
                {
                    lblTypeAction.Text = "created this book club.";
                }
                if (activity.Type == "ADDTOPIC")
                {
                     DTO.Topic topic = BookClubProcessorInstance.GetDiscussionTopicById(activity.Data);
                     lblTypeAction.Text = "added a discussion topic";
                }
                if (activity.Type == "ADDCOMMENT")
                {
                   
                    DTO.Topic topic = BookClubProcessorInstance.GetDiscussionTopicById(activity.Data);
                    lblTypeAction.Text = "added a comment to <a href='topic.aspx?id="+CID+"&tid="+topic.Id+"'>"+topic.Topic_Name+"</a>";
                  
                }
                if (activity.Type == "ADDBOOK")
                {

                    DTO.Book book = BookProcessorInstance.GetBookById(activity.Data);
                    lblTypeAction.Text = "added <a href='../book.aspx?id=" + book.Id +"'>" + book.Title + "</a> to the book shelf.";

                }
                if (activity.Type == "ADDMEETING")
                {

                    DTO.Meeting meeting = BookClubProcessorInstance.GetMeetingById(activity.Data);
                    lblTypeAction.Text = "added a meeting called <a href='meetingdetail.aspx?id=" + CID + "&mid=" + meeting.Id + "'>" + meeting.Title + "</a>.";

                }
                if (activity.Type == "UNJOIN")
                {
                    lblTypeAction.Text = "left this book club.";
                }
                lblActionDate.Text = TimeAgo(activity.Time).ToString();

                lnkMember.Text = member.Username.ToString();
                lnkMember.NavigateUrl = "../member.aspx?uid=" + member.Id;
                imgMemberAvatar.ImageUrl = "../avatars/" + member.AvatarURL.ToString();

            }
        }
        protected void grdActivityGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdActivityGrid.PageIndex = e.NewPageIndex;
            DTO.Activities activities = BookClubProcessorInstance.GetActivityById(CID);
            grdActivityGrid.DataSource = activities;
            grdActivityGrid.DataBind();
        }
        static string TimeAgo(DateTime time)
        {
            DateTime startDate = DateTime.Now;
            TimeSpan deltaMinutes = startDate.Subtract(time);
            string distance = string.Empty;
            if (deltaMinutes.TotalMinutes <= (8724 * 60))
            {
                distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
                if (deltaMinutes.Minutes < 0)
                {
                    return distance + " from  now";
                }
                else
                {
                    return distance + " ago";
                }
            }
            else
            {
                return "on " + time.ToString();
            }
        }
        static string DistanceOfTimeInWords(double minutes)
        {
            if (minutes < 1)
            {
                return "less than a minute";
            }
            else if (minutes < 50)
            {
                return Math.Round(minutes).ToString() + " minutes";
            }
            else if (minutes < 90)
            {
                return "about one hour";
            }
            else if (minutes < 1080)
            {
                return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
            }
            else if (minutes < 1440)
            {
                return "one day";
            }
            else if (minutes < 2880)
            {
                return "about one day";
            }
            else
            {
                return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
            }
        }

    }
}